$(document).ready(function() {
	//弹框模拟
//	$("#mask_body p").html("为保证数据安全，请注册后查看历史记录");
//	$("#mask_div").show();
//	$("#mask_button").click(function() {
//		window.location.href = "index.html";
//	});

	common_js.body_to_all();
	common_js.prevent_redirect();
	
	history_js.bind();
});

/**
 * 样式控制，可忽略
 */
history_js.list_content = function() {
	
	var $this = $(".main");
	var box_width = $(".list_box").width();
	var window_width = $(window).width();
	var left_px = (window_width - box_width)/2;
	var nav_height = $("#nav_bar").height();
	var top_px = nav_height+left_px;
	$this.css({
		top: top_px + "px"
	});
};

/**
 * 初始化并注册页面事件
 */
history_js.bind = function() {
	history_js.is_register();
};

/**
 * 查询用户是否已经注册
 */
history_js.is_register = function() {
	$.ajax({
		type: "POST",
		url: "", //url地址
		dataType: 'json', //示例返回数据格式为json
		//以下的接收参数默认后端与这里相同,请根据实际需要更改
		data: "",
		success: function(data) {
			//这里根据后端返回的对象做处理，这里仅做示例,返回的data
			if(data.status){
				history_js.history_result(data);
			}else{
				$("#mask_body p").html("为保证数据安全，请注册后查看历史记录");
				$("#mask_div").show();
				$("#mask_button").click(function(){
					//这里跳转至注册页
					window.location.href = "index.html";
				})
			}
		},
		error: function() {
			//util_js.show_toast_msg("服务器异常");
			//[在这里是模拟成功后获取的出现的模块列表,使用时请删掉]
			history_js.history_result(history_js.test_data);
		}
	});
}

/**
 * 创建数据卡片
 */
history_js.history_result = function(data){
	
	//这里先清空可能残留的卡片
	$(".main").empty();
	
	//循环构建多卡片
	for(var i=0;i<data.length;i++){
		var charge_item_list = "";
		for(var j=0;j<data[i].charge_item.length;j++){
			var charge_item_dom = 
				"<li class='c_gray'>"+
					"<div class='w100_ text-right'><span class='w78_ pull-left f12 pddd pt3 text-right mr5'>"+data[i].charge_item[j].pass_port
						+"</span><span class='f12'>"+data[i].charge_item[j].money+"</span></div>"+
				"</li>";
			charge_item_list = charge_item_list + charge_item_dom; 	
		}
		
		var overdue_payment_block = "";
		if(!data[i].overdue_payment || data[i].overdue_payment.length == 0){
			overdue_payment_block = "";
		}else{
			var overdue_payment_list = "";
			for(var j=0;j<data[i].overdue_payment.length;j++){
				var overdue_payment_dom = 
					"<li class='c_gray'>"+
						"<div class='w100_ text-right'><span class='w78_ pull-left f12 pddd pt3 text-right mr5'>"+data[i].overdue_payment[j].pass_port
							+"</span><span class='f12'>"+data[i].overdue_payment[j].money+"</span></div>"+
					"</li>";
				overdue_payment_list = overdue_payment_list + overdue_payment_dom; 	
			}
			overdue_payment_block = 
				"<tr class='h45'>"+
					"<td class='w80 va_top'>"+
						"<label>滞 纳 金:</label>"+
					"</td>"+
					"<td>"+
						"<ul class='va_top w100_'>"+
							overdue_payment_list+
						"</ul>"+
					"</td>"+
				"</tr>";
		}
		
		
		
		var data_card = 
		"<div class='list_box w85_ mlrauto no_border pb20'>"+
				"<div class='logo w100_ mt10 pl20 pt10'>"+
					"<img src='img/logo.png' class='logo_img'/>"+
				"</div>"+
				"<div class='w100_ pl20 pr20'>"+
					"<table class='w100_'>"+
						"<tbody>"+
							"<tr class='h30'>"+
								"<td class='w80'>"+
									"<label>日期:</label>"+
								"</td>"+
								"<td class='text-right c_gray pb5'>"+data[i].date+"</td>"+
							"</tr>"+
							"<tr class='h30'>"+
								"<td colspan='2'>"+
									"<div class='pull-left'><label>通知书编号:</label></div>"+
									"<div class='pull-right'><span class='c_gray'>"+data[i].info_book_num+"</span></div>"+
								"</td>"+
							"</tr>"+
							"<tr class='h30'>"+
								"<td class='w80'>"+
									"<label>缴 费 人 :</label>"+
								"</td>"+
								"<td class='text-right'><span class='c_gray'>"+data[i].work_place+"</span></td>"+
							"</tr>"+
							"<tr class='h45'>"+
								"<td class='w70 va_top'>"+
									"<label>收费项目:</label>"+
								"</td>"+
								"<td>"+
									"<ul class='va_top w100_'>"+
										charge_item_list+
									"</ul>"+
								"</td>"+
							"</tr>"+
								overdue_payment_block+
							"<tr class='h45'>"+
								"<td class='w80'>"+
									"<label>合计:</label>"+
								"</td>"+
								"<td class='text-right c_blue'>"+data[i].sum+"</td>"+
							"</tr>"+
						"</tbody>"+
					"</table>"+
				"</div>"+
			"</div>";
			
		$(".main").append(data_card);
	}
	//填充完Dom结构之后渲染卡片
	history_js.list_content();
}

history_js.test_data = [
	{
		info_book_num:"56789",
		work_place:"陈英东",
		charge_item:[
			{
				pass_port:"往来港澳通行证新证你sdfev",
				money:"$100"
			},
			{
				pass_port:"往来港澳通行证",
				money:"$100"
			},
		],
//		overdue_payment:[
//			{
//				pass_port:"往来港澳通行证",
//				money:"$7"
//			},
//			{
//				pass_port:"往来港澳通行证",
//				money:"$7"
//			}
//		],
		sum:"$1000",
		date:"2016年1月1日 19:20",
	},
	{
		info_book_num:"98765",
		work_place:"陈xx",
		charge_item:[
			{
				pass_port:"往来港澳通行证新证你sdfev",
				money:"$45"
			},
			{
				pass_port:"往来港澳通行证",
				money:"$368"
			},
		],
		overdue_payment:[
			{
				pass_port:"往来港澳通行证",
				money:"$7"
			},
			{
				pass_port:"往来港澳通行证",
				money:"$7"
			}
		],
		sum:"$1000",
		date:"2016年1月1日 19:20",
	},
	{
		info_book_num:"98765",
		work_place:"陈xx",
		charge_item:[
			{
				pass_port:"往来港澳通行证新证你sdfev",
				money:"$45"
			},
			{
				pass_port:"往来港澳通行证",
				money:"$368"
			},
		],
		overdue_payment:[
			{
				pass_port:"往来港澳通行证",
				money:"$7"
			},
			{
				pass_port:"往来港澳通行证",
				money:"$7"
			}
		],
		sum:"$1000",
		date:"2016年1月1日 19:20",
	}
];


